package com.mhd.databases.util;

import com.mhd.databases.common.ResultEnum;
import com.mhd.databases.pojo.TableInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.List;

/**
 * @author dengminghao
 * @version 1.0.0 @ClassName CsvUtils.java @Description csv文件工具类
 * @createTime 2020年05月23日 15:54:00
 */
public class CsvUtils {

    private static final Logger logger = LoggerFactory.getLogger(CsvUtils.class);

    public static String exportCsv(List<TableInfo> tableInfos, String finalPath) {
        FileOutputStream out = null;
        OutputStreamWriter osw = null;
        BufferedWriter bw = null;
        try {
            File finalCSVFile = new File(finalPath);
            logger.info(finalPath);

            /**
             * 获取存放文件夹路径，若不存在，则创建该路径
             */
            int splitIndex = finalPath.lastIndexOf("\\");
            String fileDir = finalPath.substring(0, splitIndex);
            logger.info(fileDir);
            File finalCSVDir = new File(fileDir);
            if (!finalCSVDir.exists()) {
                finalCSVDir.mkdirs();
            }

            out = new FileOutputStream(finalCSVFile);
            osw = new OutputStreamWriter(out, "UTF-8");
            /**手动加上BOM标识*/
            osw.write(new String(new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF}));
            bw = new BufferedWriter(osw);
            /** 往CSV中写新数据 */
            String title = "";
            title = "表名称,表大小字节,表创建时间,表注释,字段名称,字段类型,是否为空,字段注释,是否主键";
//			title = "tableName,bytes,createTime,tableComments,columnName,columnType,isNullable,columnComments，isPrimaryKey";
            bw.append(title).append("\r");

            if (tableInfos != null && !tableInfos.isEmpty()) {
                for (TableInfo data : tableInfos) {
                    bw.append(data.getTableName() + ",");
                    bw.append(data.getBytes() + ",");
                    bw.append(data.getCreateTime() + ",");
                    if (data.getTableComments() == null) {
                        bw.append(",");
                    } else {
                        bw.append(data.getTableComments() + ",");
                    }
                    bw.append(data.getColumnName() + ",");
                    bw.append(data.getColumnType() + ",");
                    bw.append(data.getIsNullable() + ",");
                    if (data.getColumnComments() == null) {
                        bw.append(",");
                    } else {
						bw.append(data.getColumnComments() + ",");
					}
                    bw.append(data.getIsPrimaryKey() + ",");
                    bw.append("\r");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEnum.FAILED.getCode();
        } finally {
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    return ResultEnum.FAILED.getCode();
                }
            }
            if (osw != null) {
                try {
                    osw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    return ResultEnum.FAILED.getCode();
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    return ResultEnum.FAILED.getCode();
                }
            }
        }
        logger.info(finalPath + "数据导出成功");
        return ResultEnum.SUCCESS.getCode();
    }
}
